package com.wenshi.web.controller.device;


import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.wenshi.common.core.domain.ModbusFrame;
import com.wenshi.common.core.domain.R;
import com.wenshi.common.utils.ChannelMap;
import com.wenshi.system.domain.ModbusMapping;
import com.wenshi.system.init.InitMappingMap;
import com.wenshi.system.service.GetDeviceStatusService;
import com.wenshi.system.service.ModbusMappingService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/modbusTest")
@Slf4j
public class modbusTestController {

    @Autowired
    private ModbusMappingService modbusMappingService;
    @Autowired
    private GetDeviceStatusService getDeviceStatusService;

    //读取保存寄存器的值,03 READ_HOLDING_REGISTERS
    @GetMapping("/read")
    public void ReadHoldingRegisters(String ip){
        Channel channel = ChannelMap.getChannel(ip);
        int transactionId = 0; // 事务标识符，应根据实际情况设置
        int protocolId = 0;
        int length = 6; // Modbus请求的长度，此处以功能码03查询为例
        int unitId = 1;
        byte functionCode = 0x03; // 读持有寄存器的功能码
        byte startAddress = 0x03; // 起始地址
        byte quantityOfRegisters = 0x03; // 查询寄存器的数量
        ByteBuf data = Unpooled.buffer();
//        data.writeByte(unitId);
//        data.writeByte(functionCode);
        data.writeShort(startAddress);
        data.writeShort(quantityOfRegisters);
        ModbusFrame requestFrame = new ModbusFrame(transactionId, protocolId, length, unitId, functionCode,data);
        channel.writeAndFlush(requestFrame);
    }
    //读取离散输入状态,02 READ_DISCRETE_INPUTS
    @GetMapping("/readDis")
    public  void  ReadDiscreteInputs(String ip){
        Channel channel = ChannelMap.getChannel(ip);
        int transactionId = 0; // 事务标识符，应根据实际情况设置
        int protocolId = 0;

        int length = 6; // Modbus请求的长度，此处以功能码03查询为例
        int unitId = 1;

        byte functionCode = 0x02; // 读持有寄存器的功能码
        byte startAddress = 0x00; // 起始地址
        byte quantityOfRegisters = 0x02; // 查询寄存器的数量
        ByteBuf data = Unpooled.buffer();
//        data.writeByte(unitId);
//        data.writeByte(functionCode);
        data.writeShort(startAddress);
        data.writeShort(quantityOfRegisters);
        ModbusFrame requestFrame = new ModbusFrame(transactionId, protocolId, length, unitId, functionCode, data);
        channel.writeAndFlush(requestFrame);
    }

    //读取线圈状态,01 READ_COILS
    @GetMapping("/readCoils")
    public  void  ReadCoils(String ip){
        Channel channel = ChannelMap.getChannel(ip);
        int transactionId = 0; // 事务标识符，应根据实际情况设置
        int protocolId = 0;

        int length = 6; // Modbus请求的长度，此处以功能码03查询为例
        int unitId = 1;

        byte functionCode = 0x01; // 读持有寄存器的功能码
        byte startAddress = 0x00; // 起始地址
        byte quantityOfRegisters = 0x02; // 查询寄存器的数量
        ByteBuf data = Unpooled.buffer();
//        data.writeByte(unitId);
//        data.writeByte(functionCode);
        data.writeShort(startAddress);
        data.writeShort(quantityOfRegisters);
        ModbusFrame requestFrame = new ModbusFrame(transactionId, protocolId, length, unitId, functionCode, data);
        channel.writeAndFlush(requestFrame);
    }

    //读取输入寄存器的值,04 READ_INPUT_REGISTERS
    @GetMapping("/readInputRegisters")
    public  void  ReadInputRegisters(String ip){
        Channel channel = ChannelMap.getChannel(ip);
        int transactionId = 0; // 事务标识符，应根据实际情况设置
        int protocolId = 0;

        int length = 6; // Modbus请求的长度，此处以功能码03查询为例
        int unitId = 1;

        byte functionCode = 0x04; // 读持有寄存器的功能码
        byte startAddress = 0x00; // 起始地址
        byte quantityOfRegisters = 0x02; // 查询寄存器的数量
        ByteBuf data = Unpooled.buffer();
//        data.writeByte(unitId);
//        data.writeByte(functionCode);
        data.writeShort(startAddress);
        data.writeShort(quantityOfRegisters);
        ModbusFrame requestFrame = new ModbusFrame(transactionId, protocolId, length, unitId, functionCode,data);
        channel.writeAndFlush(requestFrame);

    }
    //写单个线圈状态的值,05  WRITE_SINGLE_COIL(0x05),
    @GetMapping("/WriteSingleCoil")
    public  void  WriteSingleCoil(String ip){
        Channel channel = ChannelMap.getChannel(ip);
        int transactionId = 0; // 事务标识符，应根据实际情况设置
        int protocolId = 0;

        int length = 6; // Modbus请求的长度，此处以功能码03查询为例
        int unitId = 1;

        byte functionCode = 0x05; // 读持有寄存器的功能码
        byte startAddress = 0x09; //
//        byte quantityOfRegisters = (byte) 0xFF; // 查询寄存器的数量
//        byte quantityOfRegisters1 = 0x00;
        ByteBuf data = Unpooled.buffer();
//        data.writeByte(unitId);
//        data.writeByte(functionCode);
        data.writeShort(startAddress);
        data.writeByte(0xFF);
        data.writeByte(0x00);
        ModbusFrame requestFrame = new ModbusFrame(transactionId, protocolId, length, unitId, functionCode,data);
        channel.writeAndFlush(requestFrame);

    }
    //写单个保持寄存器的值,06  WRITE_SINGLE_REGISTER(0x06),
    public  void  WriteSingleRegister(){

    }
    //写多个线圈状态的值,15  WRITE_MULTIPLE_COILS(0x0F),
    public  void  WriteMultipleCoils(){

    }
    //写多个个保持寄存器的值,16  WRITE_MULTIPLE_REGISTERS(0x10),
    public  void  WriteMultipleCRegisters(){

    }
    @GetMapping("initData")
    public R initData(){
        //解析excel表数据信息  并存入数据库
        String os = System.getProperty("os.name");
        String resources = "D:/mapping.xlsx";
        InputStream stream = ResourceUtil.getStream(resources);
        ExcelReader readerSheet1 = ExcelUtil.getReader(stream, 0);
        readerSheet1.setHeaderAlias(InitMappingMap.getHeaderAlias());
        List<ModbusMapping> writeEntities = readerSheet1.readAll(ModbusMapping.class);
        log.info("获取的数据长度{}", writeEntities.size());
//        boolean b = modbusMappingService.insertModbusMappingByList(writeEntities);
//        if (b){
//            log.info("初始化modbus点表数据成功");
//        }
        List<ModbusMapping> list = modbusMappingService.list();
        for (ModbusMapping modbusMapping : list) {
            System.out.println(modbusMapping.toString());
        }
        return R.ok();
    }

}
